package mdm.led.util;

import java.io.FileOutputStream;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import mdm.led.entity.excel.ExcelFive;
import mdm.led.entity.excel.ExcelFour;
import mdm.led.entity.excel.ExcelThree;

public class ExportExcelUtil {


	private static final Logger LOGGER = LoggerFactory.getLogger(ExportExcelUtil.class);

	/**
	 * 导出督导三查Excel
	 * @return 
	 * @return
	 */
	public static Map<String, Object> exportThreeExcel(List<ExcelThree> list,String excelPath) throws Exception {
		
		String sheetName = "督导三查";
		String fileName = "督导三查";
		// 列数量
		int columnNumber = 6;
		// 对应列的宽度
		int[] columnWidth = { 15, 30, 20, 20, 20, 25 };
		// 列标题
		String[] columnName = { "序号", "三查表名称", "已完成", "未完成", "未通过", "总数" };

		// 集合对象转换成二维数组
		String[][] content = new String[list.size()][];
		for (int i = 0; i < list.size(); i++) {
			content[i] = new String[columnName.length];
			ExcelThree excel = list.get(i);
			content[i][0] = "" + (i + 1);
			content[i][1] = excel.getName();
			content[i][2] = excel.getFulfil();
			content[i][3] = excel.getNoFulfil();
			content[i][4] = excel.getNoVia();
			content[i][5] = excel.getTote();
		}

		// 导出写入到响应
		return ExportWithResponse(sheetName, fileName, columnNumber, columnWidth, columnName, content,excelPath);
	}

	/**
	 * 导出督察四必 Excel
	 * @return 
	 * @return
	 */
	public static Map<String, Object> exportFourExcel(List<ExcelFour> list,String excelPath) throws Exception {
		
		String sheetName = "督察四必";
		String fileName = "督察四必";
		// 列数量
		int columnNumber = 7;
		// 对应列的宽度
		int[] columnWidth = { 15, 30, 20, 20, 20, 25 ,25 };
		// 列标题
		String[] columnName = { "序号", "部门名称", "已完成", "未完成", "未通过", "总数" ,"督查评分" };

		// 集合对象转换成二维数组
		String[][] content = new String[list.size()][];
		for (int i = 0; i < list.size(); i++) {
			content[i] = new String[columnName.length];
			ExcelFour excel = list.get(i);
			content[i][0] = "" + (i + 1);
			content[i][1] = excel.getName();
			content[i][2] = excel.getFulfil();
			content[i][3] = excel.getNoFulfil();
			content[i][4] = excel.getNoVia();
			content[i][5] = excel.getTote();
			content[i][6] = excel.getScore();
		}

		// 导出写入到响应
		return ExportWithResponse(sheetName, fileName, columnNumber, columnWidth, columnName, content,excelPath);
	}

	/**
	 * 导出流程管控  Excel
	 * @return 
	 * @return
	 */
	public static Map<String, Object> exportFiveExcel(List<ExcelFive> list,String excelPath) throws Exception {
		
		String sheetName = "流程管控";
		String fileName = "流程管控";
		// 列数量
		int columnNumber = 4;
		// 对应列的宽度
		int[] columnWidth = { 15, 30, 25 ,20};
		// 列标题
		String[] columnName = { "序号", "表格名称", "设置时间", "填写总数" };

		// 集合对象转换成二维数组
		String[][] content = new String[list.size()][];
		for (int i = 0; i < list.size(); i++) {
			content[i] = new String[columnName.length];
			ExcelFive excel = list.get(i);
			content[i][0] = "" + (i + 1);
			content[i][1] = excel.getName();
			content[i][2] = excel.getTime();
			content[i][3] = excel.getCount();
		}

		// 导出写入到响应
		return ExportWithResponse(sheetName, fileName, columnNumber, columnWidth, columnName, content,excelPath);
	}

	/**
	 * 导出 excel xls 文件
	 * @param sheetName
	 * @param fileName
	 * @param columnNumber
	 * @param columnWidth
	 * @param columnName
	 * @param dataList
	 * @param excelPath
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> ExportWithResponse(String sheetName, String fileName, int columnNumber, int[] columnWidth,
			String[] columnName, String[][] dataList,String excelPath) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		if (columnNumber == columnWidth.length && columnWidth.length == columnName.length) {
			// 第一步，创建一个webbook，对应一个Excel文件
			HSSFWorkbook wb = new HSSFWorkbook();
			// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
			HSSFSheet sheet = wb.createSheet(sheetName);
			//sheet.setDefaultColumnWidth(18); //统一设置列宽
			for (int i = 0; i < columnNumber; i++) {
				for (int j = 0; j <= i; j++) {
					if (i == j) {
						sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
					}
				}
			}

			// 创建第1行 也就是表头
			HSSFRow row = sheet.createRow((int) 0);
			row.setHeightInPoints(25);// 设置表头高度

			// 第三步，创建表头单元格样式 以及表头的字体样式
			HSSFCellStyle style = wb.createCellStyle();
			style.setWrapText(true);// 设置自动换行
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

			style.setBottomBorderColor(HSSFColor.BLACK.index);
			style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			style.setBorderRight(HSSFCellStyle.BORDER_THIN);
			style.setBorderTop(HSSFCellStyle.BORDER_THIN);

			HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
			headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
			headerFont.setFontName("黑体"); // 设置字体类型
			headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
			style.setFont(headerFont); // 为标题样式设置字体样式

			// 第三.一步，创建表头的列
			for (int i = 0; i < columnNumber; i++) {
				HSSFCell cell = row.createCell(i);
				cell.setCellValue(columnName[i]);
				cell.setCellStyle(style);
			}

			// 第四步，创建单元格，并设置值
			for (int i = 0; i < dataList.length; i++) {
				row = sheet.createRow((int) i + 1);
				row.setHeightInPoints(17);
				// 为数据内容设置特点新单元格样式1 自动换行 上下居中
				HSSFCellStyle zidonghuanhang = wb.createCellStyle();
				zidonghuanhang.setWrapText(true);// 设置自动换行
				zidonghuanhang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

				// 设置边框
				zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);
				zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);

				// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
				HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
				zidonghuanhang2.setWrapText(true);// 设置自动换行
				zidonghuanhang2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式
				zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

				// 设置边框
				zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);
				zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);
				zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);
				HSSFCell datacell = null;
				for (int j = 0; j < columnNumber; j++) {
					datacell = row.createCell(j);
					datacell.setCellValue(dataList[i][j]);
					datacell.setCellStyle(zidonghuanhang2);
				}
			}

			// 第五步，将文件存到浏览器设置的下载位置

			// 获取当前时间数字(精确到时分秒)
			Date date = new Date();
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			String dateTime = df.format(date);

			// 下载到指定位置
			try {
				excelPath = excelPath+ "\\" + fileName + dateTime + ".xls";
				String path = fileName + dateTime + ".xls";
				FileOutputStream fout = new FileOutputStream(excelPath.trim());
				wb.write(fout);
				String str = "导出" + fileName + "成功！";
				LOGGER.info("\n-------------" + str + "------------");
				fout.close();
				map.put("path", path);
				map.put("wb", wb);
			} catch (Exception e) {
				e.printStackTrace();
				String str1 = "导出" + fileName + "失败！";
				LOGGER.info("\n-------------" + str1 + "------------");
			}

		} else {
			LOGGER.info("\n--------------列数目长度名称三个数组长度要一致---------------");
		}
		return map;

	}

}
